package com.ethan.excetion;

import com.ethan.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description:
 * @Author ethan
 * @CreateDate: 2022/02/17 下午5:20
 * @UpdateDate: 2022/02/17 下午5:20
 * @Version 1.0
 */
//@ControllerAdvice
@Slf4j
public class CustomExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public R handle(Exception e){
        //是不是自定义异常
        if(e instanceof BizException){

            BizException bizException = (BizException) e;
            log.error("[业务异常 {}]",e);

            return R.buildCodeAndMsg(bizException.getCode(),bizException.getMsg());

        }else{

            log.error("[系统异常 {}]",e);
            return R.buildError("全局异常，未知错误");
        }

    }
}
